###########################################################################
#	$Id: engineStorageSQL.p 38 2009-06-24 14:32:27Z donbeave $
###########################################################################

@CLASS
engineStorageSQL

@USE
concept/storages/conceptStorageSQL.p
pf/types/pfClass.p

@BASE
conceptStorageSQL

@OPTIONS
locals

#----- Constructor -----

@create[hshOptions]
## hshOptions.tablesPrefix
  ^BASE:create[$hshOptions]

  ^pfClass:cleanMethodArgument[hshOptions]

  $self._createOptions[$hshOptions]

  ^self._itemsClasses.add[
    $.Domains[
      $.file[engine/models/engineDomainItem.p]
      $.class[engineDomainItem]
    ]
    $.Sites[
      $.file[engine/models/engineSiteItem.p]
      $.class[engineSiteItem]
    ]
    $.Objects[
      $.file[engine/models/engineObjectItem.p]
      $.class[engineObjectItem]
    ]
  ]

  ^self._tables.add[
    $.Domains[${tablesPrefix}domains]
    $.Sites[${tablesPrefix}sites]
    $.LocalesToSites[${tablesPrefix}locales_to_sites]
  ]

  ^self._converters.add[
    $.Sites[
      $.SortOrder[^getIntConverter[]]
      $.IsPublished[^getBoolConverter[]]
    ]
  ]

  $self._domainsList[]
  $self._sitesList[]

  $self._sitesLocales[^hash::create[]]

  $self._isDomainsLoaded(false)
  $self._isSitesLoaded(false)

  ^addField[Objects;SiteID;site_id]
  ^addField[Objects;Path;path]
  ^addField[Objects;FullPath;full_path]

  ^setFieldConverter[Objects;SiteID;^getIntConverter[]]

#----- Public -----

@loadDomains[]
  $result[]
  $tItems[^_getDomainsTable[]]
  $self._domainsList[^_makeList[$tItems;Domain]]
  $self._isDomainsLoaded(true)

@loadSites[]
  $result[]
  $tItems[^_getSitesTable[]]
  $self._sitesList[^_makeList[$tItems;Site]]

#TODO сделать подгрузку списка локалей используемых сайтом только при необходимости
  $tLocales[^_getSitesLocalesTable[]]

  ^if($tLocales){
#   cleaning previous hash
    $self._sitesLocales[^hash::create[]]

    ^tLocales.menu{
      ^if(!($self._sitesLocales.[$tLocales.SiteID] is pfList)){
        $self._sitesLocales.[$tLocales.SiteID][^hash::create[]]
      }

      ^self._sitesLocales.[$tLocales.SiteID].add[
        $.[$tLocales.LocaleID](true)
      ]
    }
  }

  $self._isSitesLoaded(true)


@getDomains[hshOptions]
  ^if(!$_isDomainsLoaded){
    ^loadDomains[]
  }
  $result[$_domainsList]

@getDomainByID[intID]
  ^labourAssert:required[intID;int]

  $oDomainsList[^getDomains[]]

  ^oDomainsList.foreach[oDomain]{
    ^if($oDomain.ID == ^intID.int(0)){
      $result[$oDomain]
    }
  }

  ^pfAssert:isTrue(def $result)[Domain not found (ID: $intID).]

@getDomainByName[strName]
  ^labourAssert:required[strName;string]
  ^labourAssert:required[strName]

  $oDomainsList[^getDomains[]]

  ^oDomainsList.foreach[oDomain]{
    ^if($oDomain.Name eq $strName){
      $result[$oDomain]
    }
  }

  ^pfAssert:isTrue(def $result)[Domain not found (Name: $strName).]

@getDomainsBySiteID[intID]
  ^labourAssert:required[intID;int]

  $result[^pfArrayList::create[]]

  $oDomainsList[^getDomains[]]

  ^oDomainsList.foreach[oDomain]{
    ^if($oDomain.SiteID == ^intID.int(0)){
      ^result.add[$oDomain]
    }
  }

  ^pfAssert:isTrue(def $result)[Domains not found (SiteID: $intID).]

@getSites[hshOptions]
  ^if(!$_isSitesLoaded){
    ^loadSites[]
  }
  $result[$_sitesList]

@getSiteByID[intID]
  ^labourAssert:required[intID;int]

  $oSitesList[^getSites[]]

  ^oSitesList.foreach[oSite]{
    ^if($oSite.ID == ^intID.int(0)){
      $result[$oSite]
    }
  }

  ^pfAssert:isTrue(def $result)[Site not found (ID: $intID).]

@getSiteLocales[objSite]
  ^labourAssert:required[objSite;engineSiteItem]

  $result[$self._sitesLocales.[$objSite.ID]]

@hasLocale[objLocale;objItem]
  ^pfAssert:isTrue($objItem is engineSiteItem || $objItem is conceptBlockItem)[Item is wrong (CLASS_NAME: ${objItem.CLASS_NAME}).]

  $result(false)

  ^if($objItem is engineSiteItem){
    $result(^self._sitesLocales.[$objItem.ID].has[$objLocale.ID])
  }

@getRootObjects[objSite]
  ^if(def $objSite){
    ^labourAssert:required[objSite;engineSiteItem]
  }

  $result[^BASE:getRootObjects[
#TODO получение объектов по site_id
  ]]

@useSite[objSite]
  ^labourAssert:required[objSite;engineSiteItem]

  $self._site[$objSite]

@createObject[objType;altData;objLocale]
  $result[^BASE:createObject[$objType;$altData;$objLocale]]

  $result.site[$SITE]

#----- Private -----

@_getDomainsTable[]
  ^CSQL.table{
    SELECT
      domain_id AS ID,
      site_id AS SiteID,
      name AS Name
    FROM
      `$tables.Domains` main
  }

@_getSitesTable[]
  ^CSQL.table{
    SELECT
      site_id AS ID,
      locale_id AS LocaleID,
      sort_order AS SortOrder,
      error_object_id AS ErrorObjectID,
      is_published AS IsPublished,
      name AS Name
    FROM
      `$tables.Sites` main
  }

@_getSitesLocalesTable[]
  ^CSQL.table{
    SELECT
      site_id AS SiteID,
      locale_id AS LocaleID
    FROM
      `$tables.LocalesToSites` main
  }

@_createDomainItem[hshData]
  $result[^_createBaseItem[Domains;$hshData]]

@_createSiteItem[hshData]
  $result[^_createBaseItem[Sites;$hshData]]

#----- Properties -----

@GET_SITE[]
  $result[$_site]